import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { setStorage, getStorageObj, removeStorage } from 'src/app/utils/storage';

export interface UserInfoData {
  userId: string,
  userCode: string,
  userPwd?: string,
  userState?: string,
  userRegisterTime?: string,
  phone: string,
  name: string,
  mail: string,
  userIDCard?: string,
  headPic: string,
  sex: string,
  userIDCardFront?: string,
  userIDCardBack?: string,
  certifications: string,
  idCardType?: string,
  origin?: string,
  userIDCode?: string,
  wxNo: string,
  openId: string,
  alipayNo?: string,
  verificationCode?: string,
  regUser?: string,
  loginDate?: string,
  loginValid?: string,
  uversion?: string
}

@Injectable({
  providedIn: 'root'
})
export class UserInfoService {

  constructor() { }

  private userInfo: UserInfoData | null = null;
  userInfo$ = new Subject()

  // 获取用户信息
  getUserInfo(): UserInfoData | null {
    if (this.userInfo?.userId) {
      return this.userInfo
    } else if (getStorageObj('userInfo').userId) {
      this.userInfo = getStorageObj('userInfo')
      this.userInfo$.next(this.userInfo)
      return this.userInfo
    } else {
      return null
      // return userInfo
    }
  }
  // 设置用户信息
  setUserInfo(userInfo: UserInfoData) {
    this.userInfo = userInfo
    this.userInfo$.next(this.userInfo)
    setStorage('userInfo', this.userInfo)
  }
  // 清楚用户信息
  clearUser() {
    this.userInfo = null
    this.userInfo$.next(this.userInfo)
    removeStorage('userInfo')
  }

  // 是否登录
  getIsLogin() {
    return this.getUserInfo()?.userId ? true : false
  }
}

const userInfo: UserInfoData = {
  "userId": "b4b30e84-bde9-47fd-bb3d",
  "userCode": "157********",
  "userPwd": "381a25c8d215e6ad746df615c47cc5d5",
  "userState": '',
  "userRegisterTime": "2023-03-24 17:18:46",
  "phone": "157********",
  "name": "*赛",
  "mail": '',
  "userIDCard": "",
  "headPic": "678aeb7d-bac1-4b33-9070-d933a266466a",
  "sex": "1",
  "userIDCardFront": '',
  "userIDCardBack": '',
  "certifications": "1",
  "idCardType": "D0436CAED3CB461692C9239B2694787D",
  "origin": "gdbs",
  "userIDCode": "6a153d583635468a",
  "wxNo": "oEbLb52gI3",
  "openId": '',
  "alipayNo": '',
  "verificationCode": '',
  "regUser": '',
  "loginDate": "2023-08-30 15:37:30",
  "loginValid": "0",
  "uversion": "3.0"
}
